package com.yyfax.ph.base;

import android.content.Context;
import android.os.Bundle;
import com.yyfax.infrastructure.base.AnalyticsFragment;
import com.yyfax.ph.BuildConfig;
import com.yyfax.ph.R;
import com.yyfax.ph.base.dialog.DialogBase;
import com.yyfax.ph.common.utils.ToastUtils;

/**
 * activity的管理
 * 网络通信（成功，失败，发起网络请求）的回调dialog
 * toast的扩展
 */
public abstract class BaseFragment extends AnalyticsFragment {
	public Context context;

	private DialogBase mTipsDialog;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		context = getActivity();
	}

	@Override
	public void onDestroy() {
		if (mTipsDialog != null && mTipsDialog.isShowing()) {
			mTipsDialog.dismiss();
		}
		mTipsDialog = null;

		context = null;
		super.onDestroy();
	}

	/**
	 * 弹出系统对话框with字符串
	 *
	 * @param tips 提示内容
	 */
	public void showTipsDialog(String tips) {
		if (mTipsDialog != null && mTipsDialog.isShowing()) {
			mTipsDialog.dismiss();
		}
		mTipsDialog = new DialogBase(context, null, tips, getResources().getString(R.string.confirm), new DialogBase.BottomOneClickListener() {
			@Override
			public void bottomOneClick(DialogBase dialog) {
				dialog.dismiss();
			}
		});
		mTipsDialog.show();
	}

	/**
	 * 踏出系统提示对话框 with 字符串id
	 *
	 * @param tips 提示内容
	 */
	public void showTipsDialog(int tips) {
		if (mTipsDialog != null && mTipsDialog.isShowing()) {
			mTipsDialog.dismiss();
		}
		mTipsDialog = new DialogBase(context, null, context.getResources().getString(tips),
				getResources().getString(R.string.confirm),
				new DialogBase.BottomOneClickListener() {
					@Override
					public void bottomOneClick(DialogBase dialog) {
						dialog.dismiss();
					}
				});
		mTipsDialog.show();
	}

	/**
	 * 踏出系统提示对话框 with 字符串id
	 *
	 * @param tips 提示内容
	 */
	public void showTipsDialog(String tips, boolean cancelAble) {
		if (mTipsDialog != null && mTipsDialog.isShowing()) {
			mTipsDialog.dismiss();
		}
		mTipsDialog = new DialogBase(context, null, tips,
				getResources().getString(R.string.confirm),
				new DialogBase.BottomOneClickListener() {
					@Override
					public void bottomOneClick(DialogBase dialog) {
						dialog.dismiss();
					}
				}, cancelAble);
		mTipsDialog.show();
	}

	/**
	 * 踏出系统提示对话框 with 字符串id
	 *
	 * @param tips 提示内容
	 */
	public void showTipsDialog(int tips, boolean cancelAble) {
		if (mTipsDialog != null && mTipsDialog.isShowing()) {
			mTipsDialog.dismiss();
		}
		mTipsDialog = new DialogBase(context, null, context.getResources().getString(tips),
				getResources().getString(R.string.confirm),
				new DialogBase.BottomOneClickListener() {
					@Override
					public void bottomOneClick(DialogBase dialog) {
						dialog.dismiss();
					}
				}, cancelAble);
		mTipsDialog.show();
	}

	public void showTipsDialog(String tips, DialogBase.BottomOneClickListener listener, boolean cancelAble) {
		if (mTipsDialog != null && mTipsDialog.isShowing()) {
			mTipsDialog.dismiss();
		}
		mTipsDialog = new DialogBase(context, null, tips,
				getResources().getString(R.string.confirm), listener, cancelAble);
		mTipsDialog.show();
	}

	public void showTipsDialog(int tipsOne, int tipsTwo, DialogBase.BottomOneClickListener listener, boolean cancelAble) {
		if (mTipsDialog != null && mTipsDialog.isShowing()) {
			mTipsDialog.dismiss();
		}
		mTipsDialog = new DialogBase(context,
				context.getString(tipsOne), context.getString(tipsTwo),
				getResources().getString(R.string.confirm), listener, cancelAble);
		mTipsDialog.show();
	}

	public void showTipsDialog(String tips, int tipsBottomOne, int tipsBottomTwo, DialogBase.BottomTwoClickListener listener) {
		if (mTipsDialog != null && mTipsDialog.isShowing()) {
			mTipsDialog.dismiss();
		}
		mTipsDialog = new DialogBase(context, null, tips, getString(tipsBottomOne), getString(tipsBottomTwo), listener);
		mTipsDialog.show();
	}

	public void showTipsDialog(String tips, int tipsBottomOne, int tipsBottomTwo, DialogBase.BottomTwoClickListener listener, boolean cancelAble) {
		if (mTipsDialog != null && mTipsDialog.isShowing()) {
			mTipsDialog.dismiss();
		}
		mTipsDialog = new DialogBase(context, "", tips,
				context.getString(tipsBottomOne), context.getString(tipsBottomTwo), listener, cancelAble);
		mTipsDialog.show();
	}

	public void showTipsDialog(String tips, String tipsBottomOne, String tipsBottomTwo, DialogBase.BottomTwoClickListener listener, boolean cancelAble) {
		if (mTipsDialog != null && mTipsDialog.isShowing()) {
			mTipsDialog.dismiss();
		}
		mTipsDialog = new DialogBase(context, "", tips,
				tipsBottomOne, tipsBottomTwo, listener, cancelAble);
		mTipsDialog.show();
	}

	protected void showToast(int id) {
		ToastUtils.showToast(getActivity(), getString(id));
	}

	protected void showToast(String text) {
		ToastUtils.showToast(getActivity(), text);
	}
}
